var _cardUtil = _interopRequireDefault(require("../../utils/card-util")), _qqmap = _interopRequireDefault(require("../../public/utils/qqmap")), _imageManager = _interopRequireDefault(require("./image-manager")), _api = _interopRequireDefault(require("../../utils/api"));

function _interopRequireDefault(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}

function _defineProperty(t, e, i) {
    return e in t ? Object.defineProperty(t, e, {
        value: i,
        enumerable: !0,
        configurable: !0,
        writable: !0
    }) : t[e] = i, t;
}

var getGroupSiteList, getModelPromise = {}, getModel = function(t) {
    return t ? (getModelPromise[t] || (getModelPromise[t] = _api.default.site.model.getsiteexistmodelv2({
        data: {
            sid: t,
            isimagetext: 0
        }
    }).then(function(t) {
        return t.data.lists;
    })), getModelPromise[t]) : Promise.resolve([]);
}, memberBindTip = function() {
    wx.showModal({
        title: "管理员还未绑定官网",
        content: "绑定后可添加产品、资讯、素材",
        showCancel: !1
    });
}, managerBindTip = function(i) {
    wx.showModal({
        title: "还未绑定官网",
        content: "绑定后可添加产品、资讯、素材",
        confirmText: "去绑定",
        success: function(t) {
            if (t.confirm) {
                var e = getCurrentPages().slice(-1)[0];
                "service" === e.__query.source ? e.$route("/managePackage/service/site/site?guid=".concat(i, "&source=service")) : e.$route("/pages/contacts/bind/bind?guid=".concat(i));
            }
        }
    });
}, Editor = {
    focused: !1,
    blur: function() {
        this.focused && (this.focused = !1, this.onBlur && this.onBlur());
    },
    focus: function() {
        this.focused || (this.focused = !0, this.onFocus && this.onFocus());
    },
    isFocusableType: function(t) {
        return -1 !== [ "title", "text", "link" ].indexOf(t);
    },
    createLocationData: function(t) {
        var e = {
            type: "location",
            data: {
                address: t.address,
                latitude: t.latitude,
                longitude: t.longitude,
                name: t.name
            }
        };
        return this.addLocationStaticMapUrl(e);
    },
    validSectionFilter: function(t) {
        return "title" === t.type || "text" === t.type ? "" !== t.data.content.trim() : "link" !== t.type || t.data.link;
    },
    fillLinkContent: function(t) {
        return t && "link" === t.type && !t.data.content.trim() && t.data.link && (t.data.content = t.data.link.title), 
        t;
    },
    addLocationStaticMapUrl: function(t) {
        var e = t.data, i = e.latitude, n = e.longitude, a = wx.getSystemInfoSync().screenWidth;
        return t && "location" === t.type && (t.url = _qqmap.default.createUrl({
            size: "".concat(a, "*150"),
            markers: "size:large|color:red|".concat(i, ",").concat(n)
        })), t;
    },
    removeLocationStaticMapUrl: function(t) {
        t && "location" === t.type && delete t.url;
    },
    parseData: function(t) {
        return t.forEach(this.addLocationStaticMapUrl), t;
    }
};

Component({
    properties: {
        sections: {
            type: Array,
            value: []
        },
        card: {
            type: Object,
            value: {},
            observe: function() {
                getModelPromise = {}, getGroupSiteList = null;
            }
        },
        titlePlaceholder: {
            type: String,
            value: "点击输入标题"
        },
        textPlaceholder: {
            type: String,
            value: "介绍你的公司及业务、产品吧"
        },
        textClickPlaceholder: {
            type: String,
            value: "点击输入文本内容"
        },
        imgUploadUrl: {
            type: String,
            value: _api.default.minicard.card.upload.url
        }
    },
    data: {
        toolbarShownIndex: "",
        isIOS: !1,
        types: [ {
            type: "title",
            desc: "标题"
        }, {
            type: "text",
            desc: "文本"
        }, {
            type: "image",
            desc: "图片"
        }, {
            type: "site",
            desc: "官网"
        }, {
            type: "assets",
            desc: "素材"
        } ]
    },
    ready: function() {
        getModelPromise = {}, getGroupSiteList = null, this.setData({
            isIOS: this.$appData.isIOS
        });
    },
    methods: {
        getData: function() {
            var t = this.data.sections;
            return t && t.length ? t = JSON.parse(JSON.stringify(t)) : [];
        },
        $$onLinkSelect: function(t, e) {
            this.$$editorUpdateSectionData({
                link: t[0]
            }, e, !0);
        },
        $$imageManagerOnChange: function(t, e) {
            var i = t.images, n = t.autoplay;
            i && i.length ? this.$$editorUpdateSectionData({
                images: i,
                autoplay: n
            }, e, !0) : this.$$editorRemoveSection(e);
        },
        $$siteManagerOnChange: function(t, e, i) {
            var n = t.type, a = t.name, o = t.lists, r = t.navList, c = t.sid;
            "assets" === n && this.$$editorAddSection({
                type: n,
                data: {
                    lists: o
                }
            }, +e), "image" === n ? this.$$editorAddSection({
                type: n,
                data: {
                    images: o,
                    autoplay: !0
                }
            }, +e) : i ? this.$$editorAddSection({
                type: n,
                data: {
                    modid: i,
                    lists: o,
                    navList: r,
                    sid: c,
                    title: a
                },
                sid: this.data.card.site.sid
            }, +e) : o && o.length ? this.$$editorUpdateSectionData({
                lists: o,
                navList: r,
                sid: c
            }, e, !0) : this.$$editorRemoveSection(e);
        },
        $$editorShowToolbar: function(t) {
            this.setData({
                toolbarShownIndex: t.currentTarget.dataset.index
            });
        },
        $$editorHideToolbar: function(t) {
            this.setData({
                toolbarShownIndex: ""
            });
        },
        $$editorCreateSection: function(t) {
            var e = t.currentTarget.dataset, i = e.type, n = e.index, a = "".concat(i.charAt(0).toUpperCase()).concat(i.slice(1));
            if ("function" != typeof this["$$editorAdd".concat(a)]) throw new Error("Editor section type not supported!");
            this["$$editorAdd".concat(a)](n), this.$$editorHideToolbar();
        },
        $$editorAddSection: function(t, i) {
            var n = t.type, a = this.data.sections;
            "assets" === n ? t.data.lists.forEach(function(t, e) {
                a.splice(i + e, 0, {
                    type: n,
                    data: {
                        asset: t
                    },
                    id: Date.now()
                });
            }) : (Object.assign(t, {
                id: Date.now()
            }), a.splice(i, 0, t)), this.setData({
                sections: a
            }), Editor.isFocusableType(t.type) && this.$$editorFocusIndex(i, t.type);
        },
        $$editorUpdateSection: function(t, e) {
            this.setData(_defineProperty({}, "sections[".concat(e, "]"), t));
        },
        $$editorUpdateSectionData: function(t, e) {
            var i = 2 < arguments.length && void 0 !== arguments[2] && arguments[2], n = this.data.sections[e];
            n && (n.data = i ? Object.assign(n.data, t) : t, this.$$editorUpdateSection(n, e));
        },
        $$editorOnRemoveTap: function(t) {
            this.$$editorRemoveSection(t.currentTarget.dataset.index);
        },
        $$editorRemoveSection: function(t) {
            this.data.sections.splice(t, 1), this.setData({
                sections: this.data.sections
            });
        },
        $$editorExchange: function(t) {
            var e, i = t.currentTarget.dataset.index, n = this.data.$$editor.sections;
            this.setData((_defineProperty(e = {}, "sections[".concat(i - 1, "]"), n[i]), _defineProperty(e, "sections[".concat(i, "]"), n[i - 1]), 
            e));
        },
        $$editorBlurIndex: function(t) {
            var e = this.data.sections;
            this.data.sections[t] && this.setData(_defineProperty({}, "sections[".concat(t, "].focus"), !1)), 
            e.filter(function(t) {
                return t.focus;
            }).length || Editor.blur();
        },
        $$editorFocusIndex: function(t, e) {
            var i = this.data, n = i.isIOS;
            i.sections[t] && this.setData(_defineProperty({}, "sections[".concat(t, "].focus"), !0)), 
            !n || "title" !== e && "link" !== e ? Editor.focus() : Editor.blur();
        },
        $$editorSyncInput: function(t) {
            var e = t.currentTarget.dataset.index;
            this.data.sections[e] && this.setData(_defineProperty({}, "sections[".concat(e, "].data.content"), t.detail.value));
        },
        $$editorAddTitle: function(t) {
            this.$$editorAddSection({
                type: "title",
                data: {
                    content: ""
                }
            }, t), this.$route("/pages/components/content-editor/content-editor?type=title&content=&index=".concat(t));
        },
        $$editorEditTitle: function(t) {
            var e = t.currentTarget.dataset, i = e.content, n = e.index, a = e.type;
            this.$route("/pages/components/content-editor/content-editor?type=".concat(a, "&content=").concat(i, "&index=").concat(n));
        },
        $$editorAddText: function(t) {
            this.$$editorAddSection({
                type: "text",
                data: {
                    content: ""
                }
            }, t), this.$route("/pages/components/content-editor/content-editor?type=text&content=&index=".concat(t));
        },
        $$editorEditText: function(t) {
            var e = t.currentTarget.dataset, i = e.content, n = e.index;
            this.$route("/pages/components/content-editor/content-editor?type=text&content=".concat(i, "&index=").concat(n));
        },
        checkGroupHasSite: function(t) {
            return this.getGroupSite(t.guid).then(function(i) {
                return i.length ? wx.fPromise("showActionSheet", {
                    itemList: i.map(function(t) {
                        return t.name;
                    })
                }).then(function(t) {
                    var e = t.tapIndex;
                    if (0 <= e) return i[e].sid;
                }) : (t.isMember ? memberBindTip() : managerBindTip(t.guid), Promise.reject());
            });
        },
        checkHasSite: function() {
            var t = this.data.card;
            return t.waitJoin ? (wx.showModal({
                content: "管理员审核通过后可用",
                showCancel: !1,
                confirmText: "知道了"
            }), Promise.reject()) : t.isGroup ? this.checkGroupHasSite(t) : _cardUtil.default.isBindSite(t) ? Promise.resolve(t.sid) : (t.isMember ? memberBindTip() : managerBindTip(t.guid), 
            Promise.reject());
        },
        getGroupSite: function(t) {
            return getGroupSiteList || (getGroupSiteList = _api.default.site.profession.minicardsitelists({
                data: {
                    guid: t
                }
            }).then(function(t) {
                return t.data;
            })), getGroupSiteList;
        },
        $$editorAddSite: function(c) {
            var s = this;
            this.checkHasSite().then(function(r) {
                getModel(r).then(function(o) {
                    var t = o.map(function(t) {
                        return t.desc;
                    });
                    t.length ? wx.showActionSheet({
                        itemList: t,
                        success: function(t) {
                            var e = o[t.tapIndex], i = e.modid, n = e.type, a = void 0 === n ? "model" : n;
                            s.$route("/pages/components/select-content/select-content?type=".concat(a, "&sid=").concat(r, "&modid=").concat(i, "&content=&index=").concat(c));
                        }
                    }) : wx.showModal({
                        content: "暂无官网栏目内容",
                        showCancel: !1
                    });
                });
            });
        },
        $$editorEditModel: function(t) {
            var e, i = t.currentTarget.dataset.index, n = this.data.sections[i], a = n.type, o = n.data, r = o.modid, c = o.lists, s = o.sid, d = o.navList, u = void 0 === d ? [] : d;
            "model" === a ? e = c.map(function(t) {
                return t.cid;
            }).join(",") : "bargaining" === a && (e = c.map(function(t) {
                return t.id;
            }).join(",")), this.$route("/pages/components/select-content/select-content?type=".concat(a, "&modid=").concat(r, "&sid=").concat(s, "&content=").concat(e, "&index=").concat(i, "&selectNav=").concat(+(1 < u.length)));
        },
        $$editorOnInputBlur: function(t) {
            this.$$editorBlurIndex(t.currentTarget.dataset.index);
        },
        $$editorOnInputFocus: function(t) {
            this.$$editorFocusIndex(t.currentTarget.dataset.index);
        },
        $$editorOnTextareaFocus: function(t) {
            var e = this, i = t.currentTarget.dataset.index, n = this.data.sections[i];
            setTimeout(function() {
                e.$$editorUpdateSectionData(Object.assign({}, n.data), i, !0);
            }, 200), this.$$editorOnInputFocus(t);
        },
        $$editorOnTextAreaAndInputBlur: function(t) {
            this.$$editorOnInputBlur(t), this.$$editorSyncInput(t);
        },
        $$editorAddImage: function(i) {
            var n = this, t = this.data, e = t.imgUploadUrl, a = t.isIOS;
            wx.fPromise("showActionSheet", {
                itemList: [ "拍照", "从手机相册选择", "从小官网素材库选择" ]
            }).then(function(t) {
                switch (t.tapIndex) {
                  case 0:
                  case 1:
                    wx.fPromise("chooseImage", {
                        sourceType: [ 0 === t.tapIndex ? "camera" : "album" ],
                        sizeType: [ a ? "compressed" : "original" ]
                    }).then(function(t) {
                        return _imageManager.default.uploadImage(e, t.tempFilePaths);
                    }).then(function(t) {
                        var e = {
                            type: "image",
                            data: {
                                images: t,
                                current: 0,
                                autoplay: !0
                            }
                        };
                        n.$$editorAddSection(e, i), n.$$imageManagerEdit(e.data, i);
                    });
                    break;

                  case 2:
                    n.checkHasSite().then(function(t) {
                        return n.$route("/pages/components/select-content/select-content?type=image&sid=".concat(t, "&index=").concat(i));
                    });
                }
            });
        },
        $$editorEditImage: function(t) {
            var e = t.currentTarget.dataset.index;
            this.$$imageManagerEdit(this.data.sections[e].data, e);
        },
        $$editorOnImageSwiperChange: function(t) {
            var e = t.currentTarget.dataset.index, i = t.detail.current;
            this.$$editorUpdateSectionData({
                current: i
            }, e, !0);
        },
        $$editorPreviewImage: function() {},
        $$editorAddLink: function(t) {
            this.$$editorAddSection({
                type: "link",
                data: {
                    content: ""
                }
            }, t);
        },
        $$editorOnLinkInput: function(t) {
            var e = t.currentTarget.dataset.index;
            if (this.data.sections[e]) {
                var i = t.detail.value;
                this.setData(_defineProperty({}, "sections[".concat(e, "].data.content"), i.slice(0, 20)));
            }
        },
        $$editorOnLinkBlur: function(t) {
            this.$$editorOnLinkInput(t), this.$$editorOnInputBlur(t);
        },
        $$editorAddAssets: function(e) {
            var i = this;
            this.checkHasSite().then(function(t) {
                return i.$route("/pages/components/select-content/select-content?type=assets&index=".concat(e, "&sid=").concat(t));
            });
        },
        $$editorPlayVideo: function(t) {
            var e = t.currentTarget.dataset.url;
            this.$route("/pages/components/video-view/video-view?url=".concat(e));
        },
        $$imageManagerEdit: function(t, e) {
            this.$$imageManagerData = t, wx.navigateTo({
                url: "/pages/components/image-manager/image-manager?key=".concat(e)
            });
        }
    }
});